<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class RateInfoProperties {

    private $arrProperties = array();
    
    public function load($path = "rateinfo.properties")
    {
        $handle = @fopen($path, "r");
        if ($handle) {
            while (($buffer = fgets($handle, 4096)) !== false) {
                if ($buffer[0] == '#')
                    continue;
                if (strpos($buffer, "=") === FALSE && strpos($buffer, ":") == FALSE)
                    continue;
                list($key, $value) = split("[:=]", $buffer);
                $this->arrProperties[$key] = $value;
            }
            if (!feof($handle)) {
                echo "Error: unexpected fgets() fail\n";
            }
            fclose($handle);
        }
//        print_r($this->arrProperties);
//        exit();
    }
    public function loadDatabase()
    {
        include "database.php";
        $conn = mysql_connect($host, $userName, $password);

        if (!$conn) {
            echo "Unable to connect to DB: " . mysql_error();
            exit;
        }

        if (!mysql_select_db($database)) {
            echo "Unable to select mydbname: " . mysql_error();
            exit;
        }

        $sql = "SELECT `Key`, `Value` FROM rateinfor";

        $result = mysql_query($sql);

        if (!$result) {
            echo "Could not successfully run query ($sql) from DB: " . mysql_error();
            exit;
        }

        if (mysql_num_rows($result) == 0) {
            echo "No rows found, nothing to print so am exiting";
            exit;
        }

        // While a row of data exists, put that row in $row as an associative array
        // Note: If you're expecting just one row, no need to use a loop
        // Note: If you put extract($row); inside the following loop, you'll
        //       then create $userid, $fullname, and $userstatus
        while ($row = mysql_fetch_assoc($result)) {
            $this->arrProperties[$row["Key"]] = $row["Value"];
        }

        mysql_free_result($result);
    }
    public function getPropertyArray()
    {
        return $this->arrProperties;
    }
    
    public function getProperty($key, $default)
    {
        if (isset($this->arrProperties[$key]))
            return $this->arrProperties[$key];
        
        return $default;
    }
    public function getTitleInsuranceLenderReissueMin($state) {
        $key = "titleinsurance.$state.lenderreissue.min";
        return floatval($this->getProperty($key, 0));
    }

    public function getTitleInsuranceLenderReissueTier($state, $tierlevel) {
        $key1 = "titleinsurance.$state.lenderreissue.$tierlevel.tier";
        $key2 = "titleinsurance.$state.lenderreissue.$tierlevel.rate";
        return new TierCost(intval($this->getProperty($key1, 0)), floatval($this->getProperty($key2, 0)));
    }
    
    public function getTitleInsuranceLenderMin($state) {
        $key = "titleinsurance.$state.lender.min";
        return floatval($this->getProperty($key, 0));
    }
    
    public function getTitleInsuranceLenderTier($state, $tierlevel) {
        $key1 = "titleinsurance.$state.lender.$tierlevel.tier";
        $key2 = "titleinsurance.$state.lender.$tierlevel.rate";
        return new TierCost(intval($this->getProperty($key1, 0)), floatval($this->getProperty($key2, 0)));
    }
    
    public function getTitleInsuranceStandardOwnerMin($state) {
        $key = "titleinsurance.$state.standardowner.min";
        return floatval($this->getProperty($key, 0));
    }

    public function getTitleInsuranceStandardOwnerTier($state, $tierlevel) {        
        $key1 = "titleinsurance.$state.standardowner.$tierlevel.tier";
        $key2 = "titleinsurance.$state.standardowner.$tierlevel.rate";        
        return new TierCost(intval($this->getProperty($key1, 0)), floatval($this->getProperty($key2, 0)));
    }

    public function getTitleInsuranceEnhancedOwnerRatio($state) {
        $key = "titleinsurance.$state.enhancedowner.ratio";
        return floatval($this->getProperty($key, 0));
    }

    public function getTitleInsuranceSimultaneousIssue($state) {
        $key = "titleinsurance.$state.simultaneousissue";
        return floatval($this->getProperty($key, 0));
    }

    private function getPurchaseFeeName($state) {
        $key = "fees.$state.purchase.name";
        return $this->getProperty($key, "");
    }

    private function getPurchaseFeeValue($state) {
        $key = "fees.$state.purchase.fee";
        return floatval($this->getProperty($key, 1));
    }

    public function getMortgageFeeName($state) {
        $key = "fees.$state.mortgage.name";
        return $this->getProperty($key, "");
    }

    private function getMortgageFeeValue($state) {
        $key = "fees.$state.mortgage.fee";
        return floatval($this->getProperty($key, 1));
    }

    public function getMortgagePrimaryResidenceFeeValue($state) {
        $key = "fees.$state.mortgage.fee.primaryresidence";
        return floatval($this->getProperty($key, 1));
    }

    public function getPurchaseFee($state) {        
        return new Fee($this->getPurchaseFeeName($state), $this->getPurchaseFeeValue($state));
    }

    public function getMortgageFee($state) {
        return new Fee($this->getMortgageFeeName($state), $this->getMortgageFeeValue($state));
    }

    public function getTitleServicesCost($state) {
        $key = "titleinsurance.$state.titleservices.fee";
        return floatval($this->getProperty($key, 1));
    }

    public function getSettlementFeeCost($state) {
        $key = "fees.$state.settlement.fee";
        return floatval($this->getProperty($key, 1));
    }

    public function getLocationSurveyCost($state) {
        $key = "fees.$state.locationsurvey.fee";
        return floatval($this->getProperty($key, 1));
    }

    public function getDoubleValue($key) {
        return floatval($this->getProperty($key, -1));
    }

    public function getIntegerValue($key) {
        return intval($this->getProperty($key, -1));
    }
}
?>
